package org.micro.test.core.servlet;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.micro.test.core.Constants;
import org.micro.test.core.model.MicroClass;
import org.micro.test.core.model.MicroMethod;
import org.springframework.util.CollectionUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * The Micro API Http Servlet
 *
 * @author lry
 */
@Slf4j
public class APIHttpServlet extends HttpServlet {

    private List<MicroClass> microClasses = new ArrayList<>();

    @Override
    public void init() throws ServletException {
        super.init();
        try {
            // 读取JSON
            List<String> jsonList = IOUtils.readLines(IOUtils.resourceToURL(Constants.INFO_JSON).openStream(), StandardCharsets.UTF_8.name());
            for (String json : jsonList) {
                microClasses.add(JSON.parseObject(json, MicroClass.class));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String path = request.getRequestURI();
        request.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        response.setHeader(Constants.CONTENT_TYPE, "application/json; charset=UTF-8");

        response.getWriter().print(JSON.toJSONString(microClasses));
    }

    /**
     * 关键词搜索
     *
     * @param keyword
     * @return
     */
    private List<MicroClass> search(String keyword) {
        String tempKeyword = keyword.toLowerCase();
        List<MicroClass> resultMicroClasses = new ArrayList<>();
        for (MicroClass microClass : microClasses) {
            // 搜索Title
            if (StringUtils.isNotBlank(microClass.getTitle()) && microClass.getTitle().toLowerCase().contains(tempKeyword)) {
                resultMicroClasses.add(microClass);
                continue;
            }

            // 搜索Name
            if (StringUtils.isNotBlank(microClass.getName()) && microClass.getName().toLowerCase().contains(tempKeyword)) {
                resultMicroClasses.add(microClass);
                continue;
            }

            // 搜索PackageName
            if (StringUtils.isNotBlank(microClass.getPackageName()) && microClass.getPackageName().toLowerCase().contains(tempKeyword)) {
                resultMicroClasses.add(microClass);
                continue;
            }

            // 搜索Author
            if (StringUtils.isNotBlank(microClass.getAuthor()) && microClass.getAuthor().toLowerCase().contains(tempKeyword)) {
                resultMicroClasses.add(microClass);
                continue;
            }

            // 搜索ApiNote
            if (StringUtils.isNotBlank(microClass.getApiNote()) && microClass.getApiNote().toLowerCase().contains(tempKeyword)) {
                resultMicroClasses.add(microClass);
                continue;
            }

            List<MicroMethod> microMethodList = microClass.getMethods();
            if (!CollectionUtils.isEmpty(microMethodList)) {
                Boolean flag = false;
                for (MicroMethod microMethod : microMethodList) {
                    // 搜索Title
                    if (StringUtils.isNotBlank(microMethod.getTitle()) && microMethod.getTitle().toLowerCase().contains(tempKeyword)) {
                        flag = true;
                        break;
                    }

                    // 搜索Name
                    if (StringUtils.isNotBlank(microMethod.getName()) && microMethod.getName().toLowerCase().contains(tempKeyword)) {
                        flag = true;
                        break;
                    }

                    // 搜索Author
                    if (StringUtils.isNotBlank(microMethod.getAuthor()) && microMethod.getAuthor().toLowerCase().contains(tempKeyword)) {
                        flag = true;
                        break;
                    }

                    // 搜索ApiNote
                    if (StringUtils.isNotBlank(microMethod.getApiNote()) && microMethod.getApiNote().toLowerCase().contains(tempKeyword)) {
                        flag = true;
                        break;
                    }
                }

                if (flag) {
                    resultMicroClasses.add(microClass);
                }
            }
        }

        return resultMicroClasses;
    }

}